package scutiger.task;

import com.esotericsoftware.reflectasm.ConstructorAccess;
import org.apache.log4j.Logger;
import scutiger.util.LEIClassLoader;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LEI
 * Created by LEI on 2020/6/19.
 */
public class TempThread extends Thread {
    private static Logger logger = Logger.getLogger(TempThread.class.getName());
    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(1200000);
                logger.info("------------------------开始进行实例缓存整理------------------------");
                Set<String> secondSet = LEIClassLoader.secondConstructs.keySet();
                Set<String> firstSet = LEIClassLoader.firstConstructs.keySet();
                HashMap<String, ConstructorAccess> hashMap = new HashMap<>();
                Map<String, Integer> count = LEIClassLoader.count;
                String[] temp = new String[secondSet.size() + firstSet.size()];

                hashMap.putAll(LEIClassLoader.secondConstructs);
                hashMap.putAll(LEIClassLoader.firstConstructs);

                int index = 0;
                for (String s : secondSet) {
                    temp[index] = s;
                    ++index;
                }
                for (String s : firstSet) {
                    temp[index] = s;
                    ++index;
                }

                // 选择排序
                for (int i = 0; i < temp.length-1; i++) {
                    String maxKey = temp[i];
                    int maxIndex = i;
                    //查找最大的key
                    for (int j = i+1; j < temp.length; j++) {
                        String currentKey = temp[j];
                        if(count.get(currentKey) > count.get(maxKey)){
                             maxKey = currentKey;
                             maxIndex = j;
                        }
                    }
                    //判断 进行交换
                    if(maxIndex != i){
                        String tempKey = temp[i];
                        temp[i] = temp[maxIndex];
                        temp[maxIndex] = tempKey;
                    }
                }

                int length = temp.length > 100 ? 100 : temp.length;
                int index2 = 0;
                synchronized (LEIClassLoader.firstConstructs) {
                    LEIClassLoader.firstConstructs.clear();
                    // 将前100个放入一级缓存
                    for (int i = 0; i < length; i++) {
                        String key = temp[i];
                        LEIClassLoader.firstConstructs.put(key, hashMap.get(key));
                        index2 = i;
                    }
                }
                synchronized (LEIClassLoader.secondConstructs) {
                    LEIClassLoader.secondConstructs.clear();
                    // 将其余的放入二级缓存
                    for (int i = index2+1; i < temp.length; i++) {
                        String key = temp[i];
                        LEIClassLoader.secondConstructs.put(key, hashMap.get(key));
                    }
                }
                logger.info("------------------------结束实例缓存整理------------------------");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
